{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义期末公共题函数\n",
    "def question1():\n",
    "import datetime\n",
    "\n",
    "# 获取用户输入的年、月、日\n",
    "year = int(input(\"请输入年份：\"))\n",
    "month = int(input(\"请输入月份：\"))\n",
    "day = int(input(\"请输入日期：\"))\n",
    "\n",
    "# 创建一个datetime对象\n",
    "date_input = datetime.date(year, month, day)\n",
    "\n",
    "# 使用timetuple()方法将日期转换为struct_time，并获取tm_yday属性\n",
    "# tm_yday表示一年中的第几天（1-366）\n",
    "day_of_year = date_input.timetuple().tm_yday\n",
    "\n",
    "# 打印结果\n",
    "print(f\"{year}年{month}月{day}日是这一年的第{day_of_year}天。\")\n",
    "def question2():\n",
    "def prime_factorization(n):\n",
    "    factors = []\n",
    "    # 处理2的倍数\n",
    "    while n % 2 == 0:\n",
    "        factors.append(2)\n",
    "        n = n // 2\n",
    "    # 处理奇数倍数\n",
    "    for i in range(3, int(n**0.5) + 1, 2):\n",
    "        while n % i == 0:\n",
    "            factors.append(i)\n",
    "            n = n // i\n",
    "    # 如果n是一个质数且大于2\n",
    "    if n > 2:\n",
    "        factors.append(n)\n",
    "    # 将结果转换为字符串并打印\n",
    "    print(f\"{factors[0]}\", end=\"\")  # 打印第一个因数，不带乘号\n",
    "    for factor in factors[1:]:\n",
    "        print(f\"*{factor}\", end=\"\")  # 打印后续的因数，带乘号\n",
    "    print()  # 换行\n",
    "\n",
    "# 读取用户输入\n",
    "try:\n",
    "    num = int(input(\"请输入一个正整数: \"))\n",
    "    if num <= 0:\n",
    "        raise ValueError(\"请输入一个正整数。\")\n",
    "    prime_factorization(num)\n",
    "except ValueError as e:\n",
    "    print(e)\n",
    "\n",
    "def question3():\n",
    "def reverse_and_count_digits(number):\n",
    "    # 将输入的数字转换为字符串，以便我们可以访问每个数字\n",
    "    number_str = str(number)\n",
    "    \n",
    "    # 计算数字的位数\n",
    "    digit_count = len(number_str)\n",
    "    \n",
    "    # 逆序打印各位数字\n",
    "    reversed_digits = number_str[::-1]\n",
    "    print(\"逆序数字:\", reversed_digits)\n",
    "    \n",
    "    # 输出数字的位数\n",
    "    print(\"位数:\", digit_count)\n",
    "\n",
    "# 读取用户输入\n",
    "try:\n",
    "    user_input = input(\"请输入一个不多于6位的正整数: \")\n",
    "    \n",
    "    # 检查输入是否为正整数且不超过6位\n",
    "    if user_input.isdigit() and 1 <= len(user_input) <= 6:\n",
    "        number = int(user_input)\n",
    "        reverse_and_count_digits(number)\n",
    "    else:\n",
    "        print(\"输入无效，请输入一个不多于6位的正整数。\")\n",
    "except ValueError:\n",
    "    print(\"输入无效，请输入一个不多于6位的正整数。\")\n",
    "\n",
    "def question4():\n",
    "def calculate_series_sum(a, n):\n",
    "    # 将数字a转换为字符串，方便后续操作\n",
    "    a_str = str(a)\n",
    "    \n",
    "    # 初始化总和为0\n",
    "    total_sum = 0\n",
    "    \n",
    "    # 遍历从1到n的每个数，生成对应的字符串形式并转换为整数，然后加到总和中\n",
    "    for i in range(1, n + 1):\n",
    "        # 生成形如'a', 'aa', 'aaa', ...的字符串\n",
    "        term_str = a_str * i\n",
    "        # 将字符串转换为整数\n",
    "        term = int(term_str)\n",
    "        # 加到总和中\n",
    "        total_sum += term\n",
    "    \n",
    "    return total_sum\n",
    "\n",
    "# 从用户那里获取输入\n",
    "a = int(input(\"请输入一个数字a: \"))\n",
    "n = int(input(\"请输入要相加的数的个数n: \"))\n",
    "\n",
    "# 计算并打印结果\n",
    "result = calculate_series_sum(a, n)\n",
    "print(f\"数列的和为: {result}\")\n",
    "def question5():\n",
    "def fibonacci_sequence(n):\n",
    "    # 生成斐波那契数列的前n项\n",
    "    fib_seq = [1, 1]  # 初始化斐波那契数列的前两项\n",
    "    for i in range(2, n):\n",
    "        next_fib = fib_seq[-1] + fib_seq[-2]  # 计算下一项\n",
    "        fib_seq.append(next_fib)  # 将下一项添加到数列中\n",
    "    return fib_seq\n",
    "\n",
    "def fraction_series_sum(n):\n",
    "    # 生成斐波那契数列的前n+1项\n",
    "    fib_seq = fibonacci_sequence(n + 1)\n",
    "    \n",
    "    # 计算分数序列的前n项之和\n",
    "    series_sum = 0\n",
    "    for i in range(1, n + 1):  # 注意从1开始，因为我们需要两个相邻的斐波那契数来计算分数\n",
    "        numerator = fib_seq[i]  # 分子是当前的斐波那契数\n",
    "        denominator = fib_seq[i - 1]  # 分母是前一个斐波那契数\n",
    "        fraction = numerator / denominator  # 计算分数\n",
    "        series_sum += fraction  # 将分数加到总和中\n",
    "    \n",
    "    return series_sum\n",
    "\n",
    "# 从键盘获取输入\n",
    "n = int(input(\"请输入要计算的项数N: \"))\n",
    "\n",
    "# 计算并打印结果\n",
    "result = fraction_series_sum(n)\n",
    "print(f\"分数序列的前{n}项之和为: {result}\")\n",
    "\n",
    "def question6():\n",
    "import math\n",
    "\n",
    "# 遍历可能的整数x\n",
    "for x in range(1, 100000):  # 设定一个合理的上限，以减少计算时间\n",
    "    # 检查x + 100是否为完全平方数\n",
    "    sqrt_100 = int(math.sqrt(x + 100))\n",
    "    if sqrt_100 * sqrt_100 != x + 100:\n",
    "        continue\n",
    "    \n",
    "    # 检查x + 368是否为完全平方数\n",
    "    sqrt_368 = int(math.sqrt(x + 368))\n",
    "    if sqrt_368 * sqrt_368 == x + 368:\n",
    "        # 如果两个条件都满足，则打印出x\n",
    "        print(f\"找到的特殊整数是: {x}\")\n",
    "        break  # 找到后退出循环\n",
    "\n",
    "def question7():\n",
    "def fibonacci_rabbits(months):\n",
    "    # 初始化斐波那契数列的前两项\n",
    "    if months < 1:\n",
    "        return 0\n",
    "    elif months == 1 or months == 2:\n",
    "        return 1\n",
    "    \n",
    "    # 使用循环计算斐波那契数列\n",
    "    prev1, prev2 = 1, 1  # 前两个月的兔子总数\n",
    "    current = 0  # 当前月的兔子总数（初始化为0，将在循环中计算）\n",
    "    \n",
    "    for month in range(3, months + 1):\n",
    "        current = prev1 + prev2  # 当前月的兔子总数是前两个月的兔子总数之和\n",
    "        prev2 = prev1  # 更新前两个月的兔子总数（将前一个月的总数移到前两个月的位置）\n",
    "        prev1 = current  # 更新前两个月的兔子总数（将当前月的总数移到前一个月的位置）\n",
    "    \n",
    "    return current  # 返回最后一个月的兔子总数\n",
    "\n",
    "# 输入要计算的月份数\n",
    "months = int(input(\"请输入要计算的月份数: \"))\n",
    "# 计算并输出每个月的兔子总数\n",
    "print(f\"第{months}个月的兔子总数为: {fibonacci_rabbits(months)}对\")\n",
    "\n",
    "def question8():\n",
    "# 定义一个函数来计算3x3矩阵的对角线元素之和\n",
    "def diagonal_sum(matrix):\n",
    "    # 初始化对角线元素之和为0\n",
    "    sum_diagonal = 0\n",
    "    \n",
    "    # 遍历矩阵的行和列\n",
    "    for i in range(3):  # 3x3矩阵有3行\n",
    "        for j in range(3):  # 3x3矩阵有3列\n",
    "            # 检查元素是否位于对角线上\n",
    "            if i == j:\n",
    "                # 如果是，则将其加到总和中\n",
    "                sum_diagonal += matrix[i][j]\n",
    "    \n",
    "    # 返回对角线元素之和\n",
    "    return sum_diagonal\n",
    "\n",
    "# 从用户那里获取3x3矩阵的输入\n",
    "matrix = []\n",
    "print(\"请输入一个3x3矩阵（每行输入3个数字，用空格分隔）:\")\n",
    "for _ in range(3):\n",
    "    row = list(map(int, input().split()))\n",
    "    # 确保每行有3个数字\n",
    "    if len(row) != 3:\n",
    "        print(\"错误：每行必须包含3个数字。\")\n",
    "        exit()\n",
    "    matrix.append(row)\n",
    "\n",
    "# 计算并打印对角线元素之和\n",
    "result = diagonal_sum(matrix)\n",
    "print(f\"矩阵对角线元素之和为: {result}\")\n",
    "\n",
    "def question9():\n",
    "def rotate_list(nums, m):\n",
    "    # 确保m在有效范围内（0到len(nums)之间）\n",
    "    m = m % len(nums)  # 如果m大于列表长度，则取模得到实际移动的位数\n",
    "    \n",
    "    # 使用切片和拼接来旋转列表\n",
    "    rotated_nums = nums[-m:] + nums[:-m]\n",
    "    \n",
    "    return rotated_nums\n",
    "\n",
    "# 示例用法\n",
    "nums = [1, 2, 3, 4, 5, 6, 7]\n",
    "m = 3\n",
    "rotated = rotate_list(nums, m)\n",
    "print(rotated)  # 输出: [5, 6, 7, 1, 2, 3, 4]\n",
    "\n",
    "def question10():\n",
    "def series_sum(n):\n",
    "    # 定义一个内部函数，用于计算偶数情况下的数列和\n",
    "    def even_sum(n):\n",
    "        return sum(1 / i for i in range(2, n + 1, 2))\n",
    "    \n",
    "    # 定义一个内部函数，用于计算奇数情况下的数列和\n",
    "    def odd_sum(n):\n",
    "        return sum(1 / i for i in range(1, n + 1, 2))\n",
    "    \n",
    "    # 根据n的奇偶性调用相应的函数\n",
    "    if n % 2 == 0:\n",
    "        return even_sum(n)\n",
    "    else:\n",
    "        return odd_sum(n)\n",
    "\n",
    "# 示例用法\n",
    "print(series_sum(4))  # 输出: 0.5 + 0.25 = 0.75\n",
    "print(series_sum(5))  # 输出: 1.0 + 0.3333... + 0.2 = 1.5333...\n",
    "\n",
    "\n",
    "# 显示菜单并获取用户选择\n",
    "print(\"期末公共题选择：\")\n",
    "print(\"1.\")\n",
    "print(\"2.\")\n",
    "print(\"3.\")\n",
    "print(\"4.\")\n",
    "print(\"5.\")\n",
    "print(\"6.\")\n",
    "print(\"7.\")\n",
    "print(\"8.\")\n",
    "print(\"9.\")\n",
    "print(\"10.\")\n",
    "print(\"11.退出\")\n",
    "\n",
    "\n",
    "choice = input(\"请输入题目编号（1/2/3/4）：\")\n",
    "\n",
    "# 根据用户选择运行相应的函数或退出程序\n",
    "if choice == '1':\n",
    "    question1()\n",
    "elif choice == '2':\n",
    "    question2()\n",
    "elif choice == '3':\n",
    "    question3()\n",
    "elif choice == '4':\n",
    "    question4()\n",
    "elif choice == '5':\n",
    "    question5()\n",
    "elif choice == '6':\n",
    "    question6()\n",
    "elif choice == '7':\n",
    "    question7()\n",
    "elif choice == '8':\n",
    "    question8()\n",
    "elif choice == '9':\n",
    "    question9()\n",
    "elif choice == '10':\n",
    "    question10()\n",
    "elif choice == '11':\n",
    "    print(\"感谢使用，再见！\")\n",
    "else:\n",
    "    print(\"无效的输入，请重新输入有效的题目编号。\")\n",
    "    # 这里可以添加一个循环让用户重新选择，但在这个示例中我们省略了它。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.14.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
